package com.rtx.edi.platform.tableconfig.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.ediapi.bean.EdiApi;
import com.rtx.edi.platform.ediapi.service.EdiApiService;
import com.rtx.edi.platform.tableconfig.bean.EdiTableConfig;
import com.rtx.edi.platform.tableconfig.service.EdiTableConfigService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@SuppressWarnings("all")
@Api(tags = {"表配置"})
@RestController
@RequestMapping("/v1/ediTableConfig")
public class EdiTableConfigController {
	
	@Autowired
	EdiTableConfigService ediTableConfigService;
	
	@Autowired
	EdiApiService ediApiService;
	
	
    @ApiOperation(value = "获取EDIApi父节点列表", notes = "获取EDIApi父节点列表")
    @PostMapping("/queryParentNode")
    public EdiResponse queryParentNode(@RequestBody EdiTableConfig dto){
    	
       	
    	List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		Map<String,Object> rootNode = new HashMap<String, Object>();
		rootNode.put("label", "根节点");
		rootNode.put("value", -1);
		list.add(rootNode);
		
		LambdaQueryWrapper<EdiTableConfig> tableConfig = Wrappers.lambdaQuery();
		tableConfig.eq(EdiTableConfig::getApiid, dto.getApiid());
		List<EdiTableConfig> ediapiList = ediTableConfigService.list(tableConfig);
		
		for(EdiTableConfig name:ediapiList) {
			Map<String,Object> temp = new HashMap<String, Object>();
			temp.put("label", name.getName());
			temp.put("value", name.getId());
			list.add(temp);
		}
		
        return EdiResponse.success().data("item", list);
    }
	

    /**
     * 获取表配置
     * @return
     */
    @ApiOperation(value = "获取表配置", notes = "分页获取表配置")
    @PostMapping("selectEdiTableConfigList")
    public EdiResponse selectEdiTableConfigList(@RequestBody EdiTableConfig dto){
        Page<EdiTableConfig> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
        ediTableConfigService.selectList(pageParam,dto);
        return EdiResponse.success().data("page",pageParam);
    }
    
    
    /**
     * 查询表配置
     * @return
     */
    @ApiOperation(value = "查询表配置", notes = "根据ID 查询表配置")
    @PostMapping("selectEdiTableConfigById")
    public EdiResponse selectEdiApiById(@RequestBody EdiTableConfig dto){
        return EdiResponse.success().data("item",ediTableConfigService.getById(dto.getId()));
    }

    /**
     * 新增表配置
     * @return
     */
    @ApiOperation(value = "新增表配置", notes = "新增表配置")
    @PostMapping("addEdiTableConfig")
    public EdiResponse addEdiTableConfig(@RequestBody EdiTableConfig dto){
        return ediTableConfigService.addEdiTableConfig(dto);
    }

    /**
     * 修改表配置
     * @return
     */
    @ApiOperation(value = "修改表配置", notes = "根据ID表配置")
    @PostMapping("editEdiTableConfig")
    public EdiResponse editEdiTableConfig(@RequestBody EdiTableConfig dto){
    	ediTableConfigService.updateById(dto);
        return  EdiResponse.success();
    }
    
    /**
     * 批量删除Edi开放接口创建配置
     * @return
     */
    @ApiOperation(value = "批量删除 表配置", notes = "批量删除 表配置")
    @PostMapping("deleteEdiTableConfig")
    public EdiResponse deleteEdiTableConfigByIds(@RequestBody List<EdiTableConfig> list){
        List<Long> ids = list.stream().map(p -> p.getId()).distinct().collect(Collectors.toList());
        return  ediTableConfigService.removeBatchByIds(ids) ? EdiResponse.success() : EdiResponse.failed();
    }

}
